package cz.via.facepalm.calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cz.via.facepalm.shared.CUser;

public class LoginActivity extends Activity 
{
	Handler handler;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		handler = new Handler(Looper.getMainLooper());
				
		Button loginBtn = (Button) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{			
				
				EditText usernameInput = (EditText)findViewById(R.id.username);
				EditText passwordInput = (EditText)findViewById(R.id.password);
				String username = usernameInput.getText().toString();
				String password = passwordInput.getText().toString();
				
				final ProgressDialog progress = ProgressDialog.show(LoginActivity.this, "",
						"Verifying",true);
				
				ServerComunicator.loadUser(username,password,new DoneCallback<CUser>(){
				@Override
				public void done(CUser result) {
					//progress.dismiss();
					if(result!=null)
					{											
						Bundle credentials = new Bundle();							
						credentials.putSerializable("user", result);
						
						//Intent intent = new Intent(LoginActivity.this,
						//CalendarActivity.class);
						
						Intent intent = new Intent(LoginActivity.this,
								ActivityDetail.class);
						
						
						intent.putExtras(credentials);
						startActivity(intent);
					}
					else
					{	
						progress.dismiss();
					    final Runnable updateUI = new Runnable()
					    {			    	
					    	public void run()
					    	{					    					    	
					    		TextView error = (TextView)findViewById(R.id.message); 
					    		error.setText("Invalid username or password");
					    	}
					    };
					    handler.post(updateUI);
					}
				}
				});								
			}
		});	
		
		Button registerBtn = (Button) findViewById(R.id.register_btn);
		registerBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{		
				final ProgressDialog progress = ProgressDialog.show(LoginActivity.this, "",
						"Verifying",true);
				
				EditText usernameInput = (EditText)findViewById(R.id.username);
				EditText passwordInput = (EditText)findViewById(R.id.password);
				final String username = usernameInput.getText().toString();
				final String password = passwordInput.getText().toString();
				
				CUser u = new CUser();
				u.setName(username);
				u.setPass(password);
				
				ServerComunicator.createUser(u, new DoneCallback<Void>(){
					@Override
					public void done(Void result) {

						ServerComunicator.loadUser(username,password,new DoneCallback<CUser>(){
							@Override
							public void done(CUser result) {
								if(result!=null)
								{
									Bundle credentials = new Bundle();							
									credentials.putSerializable("user", result);
									
									Intent intent = new Intent(LoginActivity.this,
									CalendarActivity.class);
									intent.putExtras(credentials);
									startActivity(intent);
								}
								else
								{
									progress.dismiss();
									((TextView)findViewById(R.id.message)).setText("Invalid username or password");						
								}
							}
							});												
						
				}
				});				
								
			}
		});	
	}
}